using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.Shared;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;


namespace Marlimer.Common.Formularios
{
    public partial class frm_Exportacion : Form
    {
        private ReportDocument rptExport = new ReportDocument();
        private Boolean resul = false;

        public frm_Exportacion()
        {
            InitializeComponent();
        }

        public Boolean cargarExportacion(ReportDocument rep)
        {
            rptExport = rep;
            this.ShowDialog(); 
            return resul;
        }

        private void frm_Exportacion_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ccod_exp");
            dt.Columns.Add("cdes_exp");

            dt.Rows.Add("1", "Microsoft Excel 97-2000 (.xls)");
            dt.Rows.Add("2", "Microsoft 97-2000 solo datos (.xls)");
            dt.Rows.Add("3", "Microsoft Word (.doc)");
            dt.Rows.Add("4", "Formato de texto eriquecido (.rtf)");
            dt.Rows.Add("5", "Adobe Acrobat (.pdf)");
            dt.Rows.Add("6", "Crystal Reports (.rpt)");

            cboFormato.DataSource = dt;
            cboFormato.ValueMember = "ccod_exp";
            cboFormato.DisplayMember = "cdes_exp";
             
        }

        private void btnExportar_Click(object sender, EventArgs e)
        {
            String filtro = "";
            String nombrefile = "";
            dlgSave.Title = "Guardar Archivo";

            ExportOptions ex = new ExportOptions();

            int formato = Convert.ToInt32(cboFormato.SelectedValue);

            switch (formato)
            {
                case 1:     //Excel formato bravo
                    {
                        ex.ExportFormatType = ExportFormatType.Excel;
                        ExcelFormatOptions exo = new ExcelFormatOptions();
                        exo.ExcelAreaType = AreaSectionKind.Detail;
                        exo.ExcelUseConstantColumnWidth = false;
                        ex.ExportFormatOptions = exo;
                        filtro = "Microsoft Excel 97-2000 (.xls)|*.xls";
                        break;
                    }
                case 2:     //excel formato solo datos
                    {
                        ex.ExportFormatType = ExportFormatType.ExcelRecord;
                        filtro = "Microsoft 97-2000 solo datos (.xls)|*.xls";
                        break;
                    }
                case 3:     //MS word
                    {
                        ex.ExportFormatType = ExportFormatType.WordForWindows;
                        filtro = "Microsoft Word (.doc)|*.doc";
                        break;
                    }
                case 4:     //Texto enriquecido
                    {
                        ex.ExportFormatType = ExportFormatType.WordForWindows;
                        filtro = "Formato de texto eriquecido (.rtf)|*.rtf";
                        break;
                    }
                case 5:     //acrobat
                    {
                        ex.ExportFormatType = ExportFormatType.PortableDocFormat;
                        filtro = "Adobe acrobat (.pdf)|*.pdf";
                        break;
                    }
                case 6:     //crystal reports
                    {
                        ex.ExportFormatType = ExportFormatType.CrystalReport;
                        filtro = "Crystal Reports (.rpt)|*.rpt";
                        break;
                    }
            }

            try
            {
                dlgSave.Filter = filtro;
                dlgSave.OverwritePrompt = true;
                DialogResult diaresul = dlgSave.ShowDialog(this);

                if (diaresul == DialogResult.OK)
                {
                    nombrefile = dlgSave.FileName;
                    DiskFileDestinationOptions disk = new DiskFileDestinationOptions();
                    ex.ExportDestinationOptions = disk; 
                    ex.ExportDestinationType = ExportDestinationType.DiskFile;
                    disk.DiskFileName = nombrefile;
                    rptExport.Export(ex);
                    resul = true;
                    this.Visible = false;
                    MessageBox.Show("Se exporto el reporte satisfactoriamente.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close(); 
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                resul = false;
            }
        }
    }
}